package client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author casshern
 */
public class MainFrame extends JFrame implements ActionListener {

    private JButton create;
    private JButton join;
    private JButton exit;
    JPanel menu;

    public MainFrame() {
        this.setTitle("SpaceMiningXtreme");
        this.setResizable(false);
        ImagePanel panel = new ImagePanel("entities/images/main.png");
        this.add(panel);
        menu = new JPanel();

        menu.setBackground(null);
        menu.setOpaque(false);
        menu.setBorder(null);

        ///////////button part
        create = new JButton("Create Game");
        create.setPreferredSize(new Dimension(150, 80));
        create.addActionListener(this);

        create = new JButton("         ");//for start game
        create.addActionListener(this);
        create.setOpaque(false);
        create.setBorder(null);
        create.setContentAreaFilled(false);
        join = new JButton("         ");//for join game
        join.addActionListener(this);
        join.setOpaque(false);
        join.setBorder(null);
        join.setContentAreaFilled(false);
        exit = new JButton("         ");//for exit game
        exit.addActionListener(this);
        exit.setOpaque(false);
        exit.setBorder(null);
        exit.setContentAreaFilled(false);

        //layout of button
        GridBagLayout g2 = new GridBagLayout();
        GridBagConstraints c2 = new GridBagConstraints();

        menu.setLayout(g2);
        c2.gridwidth = GridBagConstraints.REMAINDER;
        c2.insets = new Insets(20, 0, 0, 0);
        g2.setConstraints(create, c2);
        create.setPreferredSize(new Dimension(100, 30));
        join.setPreferredSize(new Dimension(100, 30));
        exit.setPreferredSize(new Dimension(100, 30));
        menu.add(create, c2);
        g2.setConstraints(join, c2);
        menu.add(join, c2);
        GridBagLayout g = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        menu.add(exit, c2);
        panel.setLayout(g);
        c.insets = new Insets(30, 500, 0, 0);
        g.setConstraints(menu, c);
        panel.add(menu, c);
        ///
        this.setSize(1050, 680);
        this.setBackground(Color.BLACK);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == create) {
            new CreateGameFrame();
            this.dispose();
        } else if (e.getSource() == join) {
            new JoinGameFrame();
            this.dispose();
        } else if (e.getSource() == exit) {
            this.dispose();
            System.exit(1);
        }

    }

    class ImagePanel extends JPanel {

        BufferedImage image;
        /*
         * image should input in stream
         * then the button can be inserted on the picture.
         */

        public ImagePanel(String file) {
            try {
                image = ImageIO.read(new File(file));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void paint(Graphics g) {
            g.drawImage(image, 0, 0, null);
        }
    }

    public static void main(String[] args) {
        new MainFrame();
    }
}
